<template>
  <div class="content">
    <!--       添加提示信息-->
    <div class="hh4">
      <h3>项目获奖名单数据管理</h3>
    </div>
    <div class="subipt">
      <button type="button" class="btn btn-success tbtn" @click="tbtn">添加</button>
    </div>
    <hr>
    <el-table
        :data="tableData.filter(data => !search || data.award.raceOrProjectName.toLowerCase().includes(search.toLowerCase())||data.raceOrProjectName.toLowerCase().includes(search.toLowerCase()))"
        style="width: 100%">
      <el-table-column label="项目名称" width="220">
        <template slot-scope="scope">
          <!--              <i class="el-icon-time"></i>-->
          <span>{{ scope.row.award.raceOrProjectName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="奖项" width="220">
        <template slot-scope="scope">
          <!--              <i class="el-icon-time"></i>-->
          <span>{{ scope.row.award.awardName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="获奖时间" width="220">
        <template slot-scope="scope">
          <span>{{ scope.row.award.year }}-{{ scope.row.award.month }}-{{ scope.row.award.day }}</span>
        </template>
      </el-table-column>
      <el-table-column label="获奖人员" width="220">
        <template slot-scope="scope">
          <!--          <span v-for="item in scope.row.memberName">{{item}}</span>-->
          <span v-for="(item,index) in scope.row.memberName">
            {{ item }}
            <span v-if="index<scope.row.memberName.length-1">，</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column
          align="right">
        <template slot="header" slot-scope="scope">
          <el-input
              v-model="search"
              size="mini"
              placeholder="输入关键字搜索"/>
        </template>
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--      添加成员-->
    <div>
      <el-dialog title="添加数据" :visible.sync="dialogFormVisible" width="500px" height="60" style="margin-top: -40px">
        <el-form ref="form" :model="form">
          <el-form-item label="项目名称" :label-width="formLabelWidth">
            <el-input v-model="form.projectName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="奖项" :label-width="formLabelWidth">
            <el-input v-model="form.projectAward" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="获奖人员" :label-width="formLabelWidth">
            <el-checkbox-group v-model="form.projectAuthor">
              <el-checkbox :label='item.id' v-for="item in member">{{ item.memberName }}</el-checkbox>
            </el-checkbox-group>
            <!--          <el-input v-model="form.projectAuthor" autocomplete="off"></el-input>-->
          </el-form-item>
          <el-form-item label="获奖时间" :label-width="formLabelWidth">
            <el-input style="width: 100px" v-model="form.awardTimeYear" autocomplete="off" placeholder="年"></el-input>
            <el-input style="width: 100px" v-model="form.awardTimeMonths" autocomplete="off" placeholder="月"></el-input>
            <el-input style="width: 100px" v-model="form.awardTimeDay" autocomplete="off" placeholder="日"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="register">确定</el-button>
        </div>
      </el-dialog>
      <el-dialog title="修改数据" :visible.sync="dialogFormVisible1" width="500px" height="60" style="margin-top: -40px">
        <el-form ref="form" :model="form">
          <el-form-item label="项目名称" :label-width="formLabelWidth">
            <el-input v-model="form.projectName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="奖项" :label-width="formLabelWidth">
            <el-input v-model="form.projectAward" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="获奖人员" :label-width="formLabelWidth">
            <el-checkbox-group v-model="form.projectAuthor">
              <el-checkbox :label='item.id' v-for="item in member">{{ item.memberName }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="获奖时间" :label-width="formLabelWidth">
            <el-input style="width: 100px" v-model="form.awardTimeYear" autocomplete="off" placeholder="年"></el-input>
            <el-input style="width: 100px" v-model="form.awardTimeMonths" autocomplete="off" placeholder="月"></el-input>
            <el-input style="width: 100px" v-model="form.awardTimeDay" autocomplete="off" placeholder="日"></el-input>
            <!--            <el-input v-model="form.awardTime" autocomplete="off"></el-input>-->
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="register1">确定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import qs from "qs";
import {Message} from "element-ui";

export default {
  // 引入App.vue的刷新页面函数
  inject: ['reload'],
  name: "AdProjectAward",
  data() {
    return {
      projectId: '',
      // 暂存全体人员信息
      member: [],
      // imgUrl: '',
      // 存放所以项目获奖信息
      tableData: [],
      // 暂存搜索关键字
      search: '',
      //表单变量
      dialogFormVisible: false,
      dialogFormVisible1: false,
      // fileList: [],
      form: {
        projectName: '',
        projectAuthor: [],
        awardTimeYear: '',
        awardTimeMonths: '',
        awardTimeDay: '',
        projectAward: '',
      }
    }
  },
  methods: {
    // 编辑按钮
    handleEdit(index, row) {
      this.projectId = row.award.id
      this.form.projectName = row.award.raceOrProjectName;
      this.$axios({
        url: '/officialwebsite/award/selectAllMemberId',
        method: 'get',
        params: {
          awardId: row.award.id
        }
      }).then(res => {
        console.log(res)
        this.form.projectAuthor = res.obj
      })
      // this.form.projectAuthor=row.memberName
      this.form.awardTimeYear = row.award.year
      this.form.awardTimeMonths = row.award.month
      this.form.awardTimeDay = row.award.day
      this.form.projectAward = row.award.awardName
      // console.log(row.award.id)
      this.dialogFormVisible1 = !this.dialogFormVisible1
      // console.log(index, row);
    },
    // 删除按钮
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios({
          url: '/officialwebsite/award/delete',
          method: 'delete',
          headers: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data: qs.stringify({
            id: row.award.id
          }),
        }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.reload();
          // this.$router.go(0);
          // console.log(res)
        })
        // console.log(index, row);


      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    // 添加按钮
    tbtn() {
      this.form.projectName = ''
      this.form.projectAuthor = []
      this.form.awardTimeYear = ''
      this.form.awardTimeMonths = ''
      this.form.awardTimeDay = ''
      this.form.projectAward = ''
      this.dialogFormVisible = !this.dialogFormVisible
    },
    // 确定添加函数
    register() {
      if (this.form.projectAuthor.length === 0) {
        Message.error({message: '请添加获奖人员!'});
        // this.$router.go(0);
        // 非强制刷新页面
        // this.reload();
        return
      }
      this.$axios({
        url: '/officialwebsite/award/insertAward',
        method: 'POST',
        data: {
          awardClassify: '项目获奖',
          awardName: this.form.projectAward,
          year: this.form.awardTimeYear,
          month: this.form.awardTimeMonths,
          day: this.form.awardTimeDay,
          raceOrProjectName: this.form.projectName
        },
      }).then(res => {
        // alert(this.form.projectAuthor)
        // 添加获奖信息关联人员
        for (let i = 0; i < this.form.projectAuthor.length; i++) {
          this.$axios({
            url: '/officialwebsite/award/insertMember',
            method: 'POST',
            data: {
              awardId: res.obj,
              memberId: this.form.projectAuthor[i]
            }
          }).then(res => {
            // 判断请求是否完成，完成后再刷新页面，防止交互数据丢失
            if (i === this.form.projectAuthor.length - 1) {
              this.dialogFormVisible = !this.dialogFormVisible
              Message.success({message: '添加成功!'});
              // this.$router.go(0);
              // 非强制刷新页面
              this.reload();
            }
            // alert(res)
          })

        }


      })


    },
    // 确定编辑函数
    register1() {
      if (this.form.projectAuthor.length === 0) {
        Message.error({message: '请添加获奖人员!'});
        // this.$router.go(0);
        // 非强制刷新页面
        // this.reload();
        return
      }
      this.$axios({
        url: '/officialwebsite/award/updateAward',
        method: 'put',
        data: qs.stringify({
          awardClassify: "项目获奖",
          id: this.projectId,
          awardName: this.form.projectAward,
          year: this.form.awardTimeYear,
          month: this.form.awardTimeMonths,
          day: this.form.awardTimeDay,
          raceOrProjectName: this.form.projectName
        }),
      }).then(res => {
        // 删除获奖信息关联人员
        this.$axios({
          url: '/officialwebsite/award/deleteAllMember',
          method: 'delete',
          headers: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data: qs.stringify({
            awardId: this.projectId,
          })
        }).then(res => {
          // 添加关联信息人
          for (let i = 0; i < this.form.projectAuthor.length; i++) {
            this.$axios({
              url: '/officialwebsite/award/insertMember',
              method: 'POST',
              data: {
                awardId: this.projectId,
                memberId: this.form.projectAuthor[i]
              }
            }).then(res => {
              // 判断请求是否完成，完成后再刷新页面，防止交互数据丢失
              if (i === this.form.projectAuthor.length - 1) {
                this.dialogFormVisible1 = !this.dialogFormVisible1
                Message.success({message: '编辑成功!'});
                // this.$router.go(0);
                // 非强制刷新页面
                this.reload();
              }
            })
          }
        })

        console.log(res)
      })
    }
  },
  created: function () {
    // 获取全体成员
    this.$nextTick(function () {
      this.$axios({
        url: '/officialwebsite/member/selectAll',
        method: 'get',
      }).then(res => {
        this.member = res.obj
        console.log(this.member)

      })
    })
    // 获取所有项目获奖
    this.$nextTick(function () {
      this.$axios({
        url: '/officialwebsite/award/selectProjectAll',
        method: 'get',
      }).then(res => {
        this.tableData = res.obj
        console.log(res.obj)

      })
    })

  },

}
</script>

<style lang="less" scoped>
// 全局按钮
button {
  // hover动画时间
  transition-duration: 0.4s;
}

.content {
  width: 1040px;
}

button:hover {
  box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);
  background-color: white !important;
  color: black !important;
}

// 员工管理框
.use {
  opacity: 0.8;

  /* 阴影 */
  box-shadow: 0 20px 10px 0 rgba(0, 0, 0, 0.2), 0 6px 30px 0 rgba(0, 0, 0, 0.2);
  width: 100%;
  margin: 0 auto;
  //margin-top: 20px;
}

// 添加表单
.uusesub {
  width: 80%;
  margin: auto;
  margin-top: 20px;

}

.input-group {
  // 添加表单输入框
  .form-control {
    width: 95%;
    height: 35px;
  }

  // span
  .input-group-addon {
    border-radius: 4px !important;
    border-right: 0 !important;
    font-size: 15px;
    border-top-right-radius: 0 !important;
    border-bottom-right-radius: 0 !important;
  }
}

// 提示信息
.hh4 {
  width: 80%;
  margin: auto;
  text-align: center
}

// 添加按钮
.subipt {
  width: 80%;
  margin: auto;
  text-align: center;
  margin-top: 10px;

  .btn-success {
    width: 100px;
  }

}

// 员工信息栏
.table {
  thead tr th {
    width: 14.28571428571429% !important;
    background-color: #eeeeee;
    text-align: center;
    font-size: 15px;
  }

  tbody tr {
    td {
      text-align: center;
      //line-height: 33px;
      width: 14.28571428571429% !important;
      padding: 14px 0;
      border-bottom: solid 1px #e2e2e2;
    }
  }
}

// 编辑页面
.alter {
  display: none;
  // 透明度
  opacity: 0.9;
  // 圆角
  border-radius: 10px;
  background-color: #ffffff;
  border: #ebebeb solid 1px;
  box-shadow: 0px 0px 20px #ddd;
  text-align: center;
  width: 420px;
  height: 240px;

  position: fixed;
  left: 50%;
  top: 40%;
  transform: translate(-50%, -50%);
  z-index: 999;

  .form-group {
    height: 40px;
    width: 100% !important;

    .utext {
      width: 10%;

      text-align: right;
    }

    .uipt {
      width: 80%;
    }
  }
}

// 遮盖层
.login-bg {
  background-color: rgba(0, 0, 0, .3);
  display: none;
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0px;
  left: 0px;
  z-index: 20;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
